package com.ruoyi.activieadmin.service.impl;

import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.ruoyi.activieadmin.mapper.AaActiveDetailMapper;
import com.ruoyi.activieadmin.service.IAaActiveDetailService;
import com.ruoyi.activieadmin.domain.AaActiveDetail;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import org.springframework.beans.factory.annotation.Value;
import com.ruoyi.common.core.page.PageDomain;
import com.ruoyi.common.exception.ServiceException;
import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.system.service.ISysDictDataService;
import com.ruoyi.activieadmin.domain.AaActive;
import com.ruoyi.activieadmin.service.IAaActiveService;
import org.springframework.stereotype.Service;
import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.Objects;
import java.util.Map;

/**
 * @Description  
 * @Author kemonvvv
 * @Date 2023-06-07 
 */
@Slf4j
@Service
@Transactional(rollbackFor = Exception.class)
public class AaActiveDetailServiceImpl implements IAaActiveDetailService {


    @Resource
    private AaActiveDetailMapper aaActiveDetailMapper;
    
    
    @Resource
    private IAaActiveService aaActiveService;

    
    
    /**
     * 全查询
     * 
     * @param pageDomain 分页信息
     * @param aaActiveDetail 要查询的实体类
     * @return 结果
     */
    @Override
    public Page<AaActiveDetail> getAaActiveDetailPage(PageDomain pageDomain, AaActiveDetail aaActiveDetail , AaActive aaActive) {
        if (Objects.isNull(aaActiveDetail)) {
            return new Page<>();
        }
        try {
            LambdaQueryWrapper<AaActiveDetail> queryWrapper = new LambdaQueryWrapper<>(new AaActiveDetail());
            //if(!Objects.isNull(aaActiveDetail.getName())){
            //	queryWrapper.like(AaActiveDetail::getName,aaActiveDetail.getName());
            //}
            

            if (!Objects.isNull(aaActiveDetail.getId())) {
                queryWrapper.eq(AaActiveDetail::getId, aaActiveDetail.getId());
            }

            if (!Objects.isNull(aaActiveDetail.getAaActiveId())) {
                queryWrapper.eq(AaActiveDetail::getAaActiveId, aaActiveDetail.getAaActiveId());
            }

            aaActive.setBaseEntityNull();
            aaActive.setId(null);
            if (!PageDomain.checkObjAllFieldsIsNull(aaActive)) {
                List<Long> idList = aaActiveService.getIdListByAaActive(aaActive);
                queryWrapper.in(AaActiveDetail::getAaActiveId, idList);
            }


            if (!Objects.isNull(aaActiveDetail.getGameplayType())) {
                queryWrapper.like(AaActiveDetail::getGameplayType, aaActiveDetail.getGameplayType());
            }

            if (!Objects.isNull(aaActiveDetail.getDetailText())) {
                queryWrapper.like(AaActiveDetail::getDetailText, aaActiveDetail.getDetailText());
            }

            if (!Objects.isNull(aaActiveDetail.getStatus())) {
                queryWrapper.like(AaActiveDetail::getStatus, aaActiveDetail.getStatus());
            }

            if (!Objects.isNull(aaActiveDetail.getDelFlag())) {
                queryWrapper.like(AaActiveDetail::getDelFlag, aaActiveDetail.getDelFlag());
            }

            if (!Objects.isNull(aaActiveDetail.getCreateBy())) {
                queryWrapper.like(AaActiveDetail::getCreateBy, aaActiveDetail.getCreateBy());
            }

            if (!Objects.isNull(aaActiveDetail.getParams().get("CreateTimeBeginTime"))||!Objects.isNull(aaActiveDetail.getParams().get("CreateTimeEndTime"))) {
                if (Objects.isNull(aaActiveDetail.getParams().get("CreateTimeBeginTime"))){
                    queryWrapper.lt(AaActiveDetail::getCreateTime, aaActiveDetail.getParams().get("CreateTimeEndTime"));
                }
                if (Objects.isNull(aaActiveDetail.getParams().get("CreateTimeEndTime"))){
                    queryWrapper.gt(AaActiveDetail::getCreateTime, aaActiveDetail.getParams().get("CreateTimeBeginTime"));
                }
                if (!Objects.isNull(aaActiveDetail.getParams().get("CreateTimeBeginTime")) && !Objects.isNull(aaActiveDetail.getParams().get("CreateTimeEndTime"))) {
                    queryWrapper.between(AaActiveDetail::getCreateTime, aaActiveDetail.getParams().get("CreateTimeBeginTime"), aaActiveDetail.getParams().get("CreateTimeEndTime"));
                }
            }

            if (!Objects.isNull(aaActiveDetail.getUpdateBy())) {
                queryWrapper.like(AaActiveDetail::getUpdateBy, aaActiveDetail.getUpdateBy());
            }

            if (!Objects.isNull(aaActiveDetail.getParams().get("UpdateTimeBeginTime"))||!Objects.isNull(aaActiveDetail.getParams().get("UpdateTimeEndTime"))) {
                if (Objects.isNull(aaActiveDetail.getParams().get("UpdateTimeBeginTime"))){
                    queryWrapper.lt(AaActiveDetail::getUpdateTime, aaActiveDetail.getParams().get("UpdateTimeEndTime"));
                }
                if (Objects.isNull(aaActiveDetail.getParams().get("UpdateTimeEndTime"))){
                    queryWrapper.gt(AaActiveDetail::getUpdateTime, aaActiveDetail.getParams().get("UpdateTimeBeginTime"));
                }
                if (!Objects.isNull(aaActiveDetail.getParams().get("UpdateTimeBeginTime")) && !Objects.isNull(aaActiveDetail.getParams().get("UpdateTimeEndTime"))) {
                    queryWrapper.between(AaActiveDetail::getUpdateTime, aaActiveDetail.getParams().get("UpdateTimeBeginTime"), aaActiveDetail.getParams().get("UpdateTimeEndTime"));
                }
            }

            if (!Objects.isNull(aaActiveDetail.getRemark())) {
                queryWrapper.like(AaActiveDetail::getRemark, aaActiveDetail.getRemark());
            }
        Page<AaActiveDetail> aaActiveDetailList= aaActiveDetailMapper.selectPage(pageDomain.getPage(), queryWrapper);
        for(AaActiveDetail record:aaActiveDetailList.getRecords()){
            record.setAaActive(aaActiveService.getAaActiveById(record.getAaActiveId()));
        }

            return aaActiveDetailList;
        } catch (Exception e) {
            e.printStackTrace();
            return new Page<>();
        }
    }
    

    
    /**
     * 查询通过id
     * 
     * @param id 实体类的主键
     * @return 结果
     */
    @Override
    public AaActiveDetail getAaActiveDetailById(Long id) {
        if (Objects.isNull(id)) {
            return new AaActiveDetail();
        }
        try {
            AaActiveDetail aaActiveDetail = aaActiveDetailMapper.selectById(id);
            aaActiveDetail.setAaActive(aaActiveService.getAaActiveById(aaActiveDetail.getAaActiveId()));
            if (Objects.isNull(aaActiveDetail)){
                aaActiveDetail = new AaActiveDetail();
            }
            return aaActiveDetail;

        } catch (Exception e) {
            e.printStackTrace();
            return new AaActiveDetail();
        }
    }


    
    
    /**
     * 通过类查询的属性值 id列表
     * 
     * @param aaActiveDetail 要查询的实体类
     * @return 结果
     */
    @Override
    public List<Long> getIdListByAaActiveDetail(AaActiveDetail aaActiveDetail) {
        if (Objects.isNull(aaActiveDetail)) {
            return new ArrayList<>();
        }
        try {
            LambdaQueryWrapper<AaActiveDetail> queryWrapper = new LambdaQueryWrapper<>(new AaActiveDetail());
            

            if (!Objects.isNull(aaActiveDetail.getId())) {
                queryWrapper.eq(AaActiveDetail::getId, aaActiveDetail.getId());
            }

            if (!Objects.isNull(aaActiveDetail.getAaActiveId())) {
                queryWrapper.eq(AaActiveDetail::getAaActiveId, aaActiveDetail.getAaActiveId());
            }

            if (!Objects.isNull(aaActiveDetail.getGameplayType())) {
                queryWrapper.like(AaActiveDetail::getGameplayType, aaActiveDetail.getGameplayType());
            }

            if (!Objects.isNull(aaActiveDetail.getDetailText())) {
                queryWrapper.like(AaActiveDetail::getDetailText, aaActiveDetail.getDetailText());
            }

            if (!Objects.isNull(aaActiveDetail.getStatus())) {
                queryWrapper.like(AaActiveDetail::getStatus, aaActiveDetail.getStatus());
            }

            if (!Objects.isNull(aaActiveDetail.getDelFlag())) {
                queryWrapper.like(AaActiveDetail::getDelFlag, aaActiveDetail.getDelFlag());
            }

            if (!Objects.isNull(aaActiveDetail.getCreateBy())) {
                queryWrapper.like(AaActiveDetail::getCreateBy, aaActiveDetail.getCreateBy());
            }

            if (!Objects.isNull(aaActiveDetail.getParams().get("CreateTimeBeginTime"))||!Objects.isNull(aaActiveDetail.getParams().get("CreateTimeEndTime"))) {
                if (Objects.isNull(aaActiveDetail.getParams().get("CreateTimeBeginTime"))){
                    queryWrapper.lt(AaActiveDetail::getCreateTime, aaActiveDetail.getParams().get("CreateTimeEndTime"));
                }
                if (Objects.isNull(aaActiveDetail.getParams().get("CreateTimeEndTime"))){
                    queryWrapper.gt(AaActiveDetail::getCreateTime, aaActiveDetail.getParams().get("CreateTimeBeginTime"));
                }
                if (!Objects.isNull(aaActiveDetail.getParams().get("CreateTimeBeginTime")) && !Objects.isNull(aaActiveDetail.getParams().get("CreateTimeEndTime"))) {
                    queryWrapper.between(AaActiveDetail::getCreateTime, aaActiveDetail.getParams().get("CreateTimeBeginTime"), aaActiveDetail.getParams().get("CreateTimeEndTime"));
                }
            }

            if (!Objects.isNull(aaActiveDetail.getUpdateBy())) {
                queryWrapper.like(AaActiveDetail::getUpdateBy, aaActiveDetail.getUpdateBy());
            }

            if (!Objects.isNull(aaActiveDetail.getParams().get("UpdateTimeBeginTime"))||!Objects.isNull(aaActiveDetail.getParams().get("UpdateTimeEndTime"))) {
                if (Objects.isNull(aaActiveDetail.getParams().get("UpdateTimeBeginTime"))){
                    queryWrapper.lt(AaActiveDetail::getUpdateTime, aaActiveDetail.getParams().get("UpdateTimeEndTime"));
                }
                if (Objects.isNull(aaActiveDetail.getParams().get("UpdateTimeEndTime"))){
                    queryWrapper.gt(AaActiveDetail::getUpdateTime, aaActiveDetail.getParams().get("UpdateTimeBeginTime"));
                }
                if (!Objects.isNull(aaActiveDetail.getParams().get("UpdateTimeBeginTime")) && !Objects.isNull(aaActiveDetail.getParams().get("UpdateTimeEndTime"))) {
                    queryWrapper.between(AaActiveDetail::getUpdateTime, aaActiveDetail.getParams().get("UpdateTimeBeginTime"), aaActiveDetail.getParams().get("UpdateTimeEndTime"));
                }
            }

            if (!Objects.isNull(aaActiveDetail.getRemark())) {
                queryWrapper.like(AaActiveDetail::getRemark, aaActiveDetail.getRemark());
            }

            List<AaActiveDetail> aaActiveDetails = aaActiveDetailMapper.selectList(queryWrapper);
            List<Long> idList = new ArrayList<>();
            for (AaActiveDetail aaActiveDetailNo:aaActiveDetails){
                idList.add(aaActiveDetailNo.getId());
            }
            return idList;
        } catch (Exception e) {
            e.printStackTrace();
            return new ArrayList<>();
        }
    }


    /**
     * 通过aaActive外键idList查询idList
     *
     * @param aaActiveIdList 要查询的实体类
     * @return 结果
     **/
    public List<Long> getIdListByAaActiveIdList(List<Long> aaActiveIdList) {
        if (Objects.isNull(aaActiveIdList)){
            return  new ArrayList<>();
        }
        try{
            LambdaQueryWrapper<AaActiveDetail> queryWrapper = new LambdaQueryWrapper<>(new AaActiveDetail());
            queryWrapper.in(AaActiveDetail::getAaActiveId, aaActiveIdList);
            List<AaActiveDetail> aaActiveDetails = aaActiveDetailMapper.selectList(queryWrapper);
            List<Long> idList = new ArrayList<>();
            for (AaActiveDetail aaActiveDetail:aaActiveDetails){
                idList.add(aaActiveDetail.getId());
            }
            return idList;
        }catch (Exception e){
            return new ArrayList<>();
        }
    }

    
    /**
     * 添加
     * 
     * @param aaActiveDetail 要添加的实体类
     * @return 结果
     */
    @Override
    public int insertAaActiveDetail(AaActiveDetail aaActiveDetail) {
        if (Objects.isNull(aaActiveDetail)) {
            return -1;
        }
        try {
        
            //aaActiveDetail.setSpell(PinYin.getPinYin(aaActiveDetail.getName()));
            return aaActiveDetailMapper.insert(aaActiveDetail);
        } catch (Exception e) {
            return -1;
        }
    }

    /**
     * 更新通过id进行更新一条
     * 
     * @param aaActiveDetail 实体类
     * @return 结果
     */
    @Override
    public int updateAaActiveDetailById(AaActiveDetail aaActiveDetail) {
        if (Objects.isNull(aaActiveDetail)) {
            return -1;
        }
        try {
            //aaActiveDetail.setSpell(PinYin.getPinYin(aaActiveDetail.getName()));
            return aaActiveDetailMapper.updateById(aaActiveDetail);
        } catch (Exception e) {
            return -1;
        }
    }
    
    /**
     * 删除通过id集合
     * 
     * @param idList id集合
     * @return 结果
     */
    @Override
    public int deleteAaActiveDetailByIdList(List idList){
        if (Objects.isNull(idList)) {
            return -1;
        }
        try {
            return aaActiveDetailMapper.deleteBatchIds(idList);
        } catch (Exception e) {
            return -1;
        }
    }
    
    /**
     * 查询无条件全部 用于导出
     * 
     * @return 结果
     */
    @Override
    public List<AaActiveDetail> getAaActiveDetailList() {
        try {
            LambdaQueryWrapper<AaActiveDetail> queryWrapper = new LambdaQueryWrapper<>(new AaActiveDetail());
            queryWrapper.isNotNull(AaActiveDetail::getId);
            return aaActiveDetailMapper.selectList(queryWrapper);
        } catch (Exception e) {
            e.printStackTrace();
            return new ArrayList<>();
        }
    }

}
